package com.fudan.adse.service.impl;

import com.fudan.adse.dao.CinemaDao;
import com.fudan.adse.domain.bo.CinemaBO;
import com.fudan.adse.domain.po.Room;
import com.fudan.adse.domain.po.RoomExample;
import com.fudan.adse.grpc.CinemaGrpc;
import com.fudan.adse.grpc.CinemaQuery;
import com.fudan.adse.grpc.CinemaResponse;
import com.fudan.adse.grpc.RoomResponse;
import com.fudan.adse.mbg.mapper.CinemaMapper;
import com.fudan.adse.mbg.mapper.RoomMapper;
import com.fudan.adse.utils.StringUtil;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jerry Zhang
 * create: 2022-11-19 19:15
 */
@Slf4j
@GrpcService
public class CinemaGrpcServiceImpl extends CinemaGrpc.CinemaImplBase {
    @Autowired
    private CinemaMapper cinemaMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private CinemaDao cinemaDao;

    /**
     * 根据影院 id 获取详细数据
     *
     * @return
     */
    @Override
    public void listCinemasByCondition(CinemaQuery query, StreamObserver<CinemaResponse> responseObserver) {
        List<CinemaBO> cinemas = cinemaDao.selectCinemaByIds(query.getIdList());
        List<CinemaResponse.CinemaVO> retList = new ArrayList<>();
        cinemas.forEach(cinema -> retList.add(CinemaResponse.CinemaVO.newBuilder()
                .setId(cinema.getId())
                .setName(cinema.getName())
                .setLocationDetail(StringUtil.selfOrEmpty(cinema.getLocationDetail()))
                .setStatus(cinema.getStatus())
                .setLocation(CinemaResponse.LocationVO.newBuilder()
                        .setId(cinema.getLocation().getId())
                        .setCode(StringUtil.selfOrEmpty(cinema.getLocation().getCode()))
                        .setCountry(StringUtil.selfOrEmpty(cinema.getLocation().getCountry()))
                        .setProvince(StringUtil.selfOrEmpty(cinema.getLocation().getProvince()))
                        .setCity(StringUtil.selfOrEmpty(cinema.getLocation().getCity()))
                        .setTown(StringUtil.selfOrEmpty(cinema.getLocation().getTown()))
                        .build())
                .build()));
        responseObserver.onNext(CinemaResponse.newBuilder()
                .addAllCinema(retList)
                .build());
        responseObserver.onCompleted();
    }

    @Override
    public void listRoomsByCinemaId(CinemaQuery request, StreamObserver<RoomResponse> responseObserver) {
        RoomExample example = new RoomExample();
        example.createCriteria().andCinemaIdIn(request.getIdList());
        List<Room> rooms = roomMapper.selectByExample(example);
        List<RoomResponse.RoomVO> retList = new ArrayList<>();
        rooms.forEach(room -> retList.add(RoomResponse.RoomVO.newBuilder()
                .setId(room.getId())
                .setCinemaId(room.getCinemaId())
                .setName(StringUtil.selfOrEmpty(room.getName()))
                .setCapacity(room.getCapacity())
                .setStatus(room.getStatus())
                .setDescription(StringUtil.selfOrEmpty(room.getDescription()))
                .build()));
        responseObserver.onNext(RoomResponse.newBuilder()
                .addAllRoom(retList)
                .build());
        responseObserver.onCompleted();
    }
}
